/**
 * COPYRIGHT (C) 2010 LY. ALL RIGHTS RESERVED.
 *
 * No part of this publication may be reproduced, stored in a retrieval system,
 * or transmitted, on any form or by any means, electronic, mechanical, photocopying,
 * recording, or otherwise, without the prior written permission of 3KW.
 *
 * Created By: zzqiang
 * Created On: 2013-10-14
 *
 * Amendment History:
 * 
 * Amended By       Amended On      Amendment Description
 * ------------     -----------     ---------------------------------------------
 *
 **/
package com.core.sgip.common.head;

import com.core.sgip.common.SGIPSeq;
import com.core.sgip.common.SGIPUtils;
import com.core.sgip.common.SGIPproperties;
import com.core.sgip.interf.SGIPByteData;

/**
 * 消息头部
 * 
 * @author zzqiang
 *
 */
public class SGIPMsgHead implements SGIPByteData {

	public static final int HEAD_LENGTH = 20;

	public SGIPMsgHead() {
		super();
	}

	public SGIPMsgHead(Long commandId) {
		super();
		this.commandId = commandId;
	}
	public SGIPMsgHead(Long commandId,String seq) {
		super();
		this.commandId = commandId;
		this.sequenceNumberStr = seq;
	}

	/**
	 * 消息的总长度(字节)
	 */
	private Long messageLength;
	/**
	 * 命令ID
	 */
	private Long commandId;

	private byte[] sequenceNumber = new byte[12]; // 序列号由 源节点编号+命令产生日期+命令循环序号组成

	/**
	 * 源节点编号
	 */
	private Long sourceNodeNumber = Long.valueOf("3" + SGIPproperties.AREA_CODE + SGIPproperties.COMPANY_CODE);// 4字节
																												// 为
																												// 3AAAAQQQQQ

	/**
	 * 命令产生日期-月日时分秒
	 */
	private Long commandGenerateDate; // 4字节
	/**
	 * 命令循环序号
	 */
	private Long commandNumber;// 4字节

	private String sequenceNumberStr;

	public String getSequenceNumberStr() {
		return String.valueOf(sourceNodeNumber) + String.valueOf(commandGenerateDate) + String.valueOf(commandNumber);
	}

	private Long getSourceNodeNumber() {
		return sourceNodeNumber;
	}

	public void setSourceNodeNumber(Long sourceNodeNumber) {
		this.sourceNodeNumber = sourceNodeNumber;
	}

	private Long getCommandGenerateDate() {
		return commandGenerateDate;
	}

	public void setCommandGenerateDate(Long commandGenerateDate) {
		this.commandGenerateDate = commandGenerateDate;
	}

	private Long getCommandNumber() {
		return commandNumber;
	}

	public void setCommandNumber(Long commandNumber) {
		this.commandNumber = commandNumber;
	}

	public Long getMessageLength() {
		return messageLength;
	}

	public void setMessageLength(Long messageLength) {
		this.messageLength = messageLength;
	}

	public Long getCommandId() {
		return commandId;
	}

	public void setCommandId(Long commandId) {
		this.commandId = commandId;
	}

	private byte[] getSequenceNumber() {
		return sequenceNumber;
	}

	private void setSequenceNumber(byte[] sequenceNumber) {
		this.sequenceNumber = sequenceNumber;
	}

	/**
	 * 产生命令序列号
	 */
	public void generateSequenceNumber() {
		generateDate();
		generateSeq();
		byte[] sourceNodeNumber = SGIPUtils.convertLong2FourBytes(this.sourceNodeNumber);//3073100000
		byte[] commandDate = SGIPUtils.convertLong2FourBytes(this.commandGenerateDate);//518111503
		byte[] commandNum = SGIPUtils.convertLong2FourBytes(this.commandNumber);//3
		this.sequenceNumber = SGIPUtils.mergeBytes(sourceNodeNumber, commandDate, commandNum);
	}
	public void generateSequenceNumber(Long sequenceNumber) {
		generateDate();
		generateSeq();
		
		String commandate = String.valueOf(sequenceNumber);
		byte[] sourceNodeNumber = SGIPUtils.convertLong2FourBytes(Long.valueOf("3" + SGIPproperties.AREA_CODE + SGIPproperties.COMPANY_CODE));
		byte[] commandDate = SGIPUtils.convertLong2FourBytes(Long.valueOf(commandate));
		byte[] commandNum = SGIPUtils.convertLong2FourBytes(Long.valueOf(3));
		this.sequenceNumber = SGIPUtils.mergeBytes(sourceNodeNumber, commandDate, commandNum);
		System.out.println(sequenceNumber);
	}
	public void generateSequenceNumber(String sequenceNumber) {
		generateDate(sequenceNumber);
		generateSeq(sequenceNumber);
		
		String commandate = String.valueOf(sequenceNumber.substring(10,sequenceNumber.length()-1));
		byte[] sourceNodeNumber = SGIPUtils.convertLong2FourBytes(Long.valueOf("3" + SGIPproperties.AREA_CODE + SGIPproperties.COMPANY_CODE));
		byte[] commandDate = SGIPUtils.convertLong2FourBytes(Long.valueOf(commandate));
		byte[] commandNum = SGIPUtils.convertLong2FourBytes(Long.valueOf(sequenceNumber.substring(sequenceNumber.length()-1,sequenceNumber.length())));
		this.sequenceNumber = SGIPUtils.mergeBytes(sourceNodeNumber, commandDate, commandNum);
		System.out.println(sequenceNumber);
		System.out.println(sequenceNumberStr);
	}

	private void generateDate(String seq) {
		this.commandGenerateDate = Long.valueOf(seq.substring(10));
	}
	private void generateDate() {
		this.commandGenerateDate = SGIPUtils.getCurrentDate();
	}

	private void generateSeq() {
		this.commandNumber = SGIPSeq.getSeq();
	}
	private void generateSeq(String str) {
		this.commandNumber = SGIPSeq.getSeq();
	}

	@Override
	public byte[] getByteData() {
		generateSequenceNumber();
		byte[] commandIdByte = SGIPUtils.convertLong2FourBytes(this.commandId);//3
		byte[] msgLengthByte = SGIPUtils.convertLong2FourBytes(this.messageLength);//184
		return SGIPUtils.mergeBytes(msgLengthByte, commandIdByte, this.sequenceNumber);
	}
	public byte[] getByteDatasubmitresp(String seq) {
		generateSequenceNumber(seq);
		byte[] commandIdByte = SGIPUtils.convertLong2FourBytes(this.commandId);//3
		byte[] msgLengthByte = SGIPUtils.convertLong2FourBytes(this.messageLength);//184
		return SGIPUtils.mergeBytes(msgLengthByte, commandIdByte, this.sequenceNumber);
	}
	
	
	
	public byte[] getByteData(Long sequenceNumber) {
		
		generateSequenceNumber(sequenceNumber);
		byte[] commandIdByte = SGIPUtils.convertLong2FourBytes(this.commandId);
		byte[] msgLengthByte = SGIPUtils.convertLong2FourBytes(this.messageLength);
		return SGIPUtils.mergeBytes(msgLengthByte, commandIdByte, this.sequenceNumber);
	}
	public byte[] getByteData(String sequenceNumber) {
		
		generateSequenceNumber(Long.valueOf(sequenceNumber));
		byte[] commandIdByte = SGIPUtils.convertLong2FourBytes(this.commandId);
		byte[] msgLengthByte = SGIPUtils.convertLong2FourBytes(this.messageLength);
		return SGIPUtils.mergeBytes(msgLengthByte, commandIdByte, this.sequenceNumber);
	}

	@Override
	public void initPropertiesByBytes(byte[] source) {
		byte[] messageLengthByte = new byte[4];
		byte[] commandIdByte = new byte[4];
		SGIPUtils.copyBytes(source, messageLengthByte, 1, 4, 1);
		SGIPUtils.copyBytes(source, commandIdByte, 5, 4, 1);
		this.messageLength = SGIPUtils.byte4ToLong(messageLengthByte);
		this.commandId = SGIPUtils.byte4ToLong(commandIdByte);
		SGIPUtils.copyBytes(source, this.sequenceNumber, 9, 12, 1);
		byte[] sourceNodeNumberByte = new byte[4];
		byte[] generateDateByte = new byte[4];
		byte[] numberByte = new byte[4];
		SGIPUtils.copyBytes(this.sequenceNumber, sourceNodeNumberByte, 1, 4, 1);
		SGIPUtils.copyBytes(this.sequenceNumber, generateDateByte, 5, 4, 1);
		SGIPUtils.copyBytes(this.sequenceNumber, numberByte, 9, 4, 1);
		this.sourceNodeNumber = SGIPUtils.byte4ToLong(sourceNodeNumberByte);
		this.commandGenerateDate = SGIPUtils.byte4ToLong(generateDateByte);
		this.commandNumber = SGIPUtils.byte4ToLong(numberByte);
	}

	@Override
	public String toString() {
		return "SGIPMsgHead [messageLength=" + messageLength + ", commandId=" + commandId + ", sourceNodeNumber="
				+ sourceNodeNumber + ", commandGenerateDate=" + commandGenerateDate + ", commandNumber=" + commandNumber
				+ "]";
	}

}
